$(function() {
	var userId = sessionStorage.getItem("userId");
	var username = sessionStorage.getItem("username");
	var userImg = sessionStorage.getItem("userImg");
	if(userId == null || userId == undefined || userId == 'null'){
		userId = 10000;username="管理员";userImg="static/img/fire-1.jpg";
	}else{
		userId = userId.toString();
	}
	console.log(userId);
	
    //好友资料-聊天记录-发送消息
	var friendId = $.Request("f");
	console.log(friendId)
	
	var friendName = $.Request("friendName");
	var friendImg = sessionStorage.getItem("img_"+friendId);
	
	
	$("title").text(friendName);
	if(friendId != null && friendId != undefined) {
		console.log(friendImg)
		//消息模板
		var start1 = "<li class='mui-table-view-cell mui-media'><a href='javascript:;'>";
		start1 = start1 + "<img class='mui-media-object mui-pull-left' src='"+friendImg+"' style='margin-right:0px'>";
		start1 = start1 + "<div class='mui-media-body' style='margin-top: 0%;'><p class='msg1'>";
		var start2 = "<li class='mui-table-view-cell mui-media'><a href='javascript:;'>";
		start2 = start2 + "<img class='mui-media-object mui-pull-right' src='"+userImg+"'>";
		start2 = start2 + "<div class='mui-media-body mymsg' style='overflow: visible;'><p class='msg2'>";
		//消息列表
		$.ajax({
			type: "get",
			url: apiPath + "chat/getMsg",
			data: {
				'friendId': friendId,
				'userId': userId
			},
			success: function(data) {
				console.log(data);
				if(data.code == 0) {
					for(var i = 0;i < data.list.length;i++){
						var listr;
						if(data.list[i].type == 0){
							listr = start1 + data.list[i].content;
						}
						if(data.list[i].type == 1){
							listr = start2 + data.list[i].content;
						}
						listr = listr + "<span></span></p></div></a></li>";
						$("#msg_ul").append(listr);
					}
				}
			}
		});
	}

	//建立socket连接
	if(typeof(WebSocket) != "undefined") {
        // 实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
        var socketUrl= apiPath + "/im/" + userId;
		socketUrl = socketUrl.replace("https", "ws").replace("http", "ws");
		console.log(socketUrl)
		var socket = new WebSocket(socketUrl);
		console.log(socket);
		//打开事件
		socket.onopen = function() {
			console.log("websocket已打开");
			//socket.send("这是来自客户端的消息" + location.href + new Date());
		};
		//获得消息事件
		socket.onmessage = function(msg) {
			console.log("获取消息：" + msg.data);
			var obj = JSON.parse(msg.data);
			//发现消息进入    开始处理前端触发逻辑
			if(obj.friendId == friendId){
				if(obj.code == 0){
					console.log(obj.content);
					if(obj.content != null && obj.content != undefined && obj.content != ""){
						var limsg1 = start1 + obj.content;
						limsg1 = limsg1 + "<span></span></p></div></a></li>";
						$("#msg_ul").append(limsg1);
						
						uptHeight();
					}
				}
			}
			
			
			if (obj.code == 100 || obj.code == 300){ //好友上下线更新
				
			}
		};
		//关闭事件
		socket.onclose = function() {
			layer.msg("websocket已关闭", {icon: 2});
		};
		//发生了错误事件
		socket.onerror = function() {
			layer.msg("websocket发生了错误", {icon: 2});
		}
    }else{
    	console.log("您的浏览器不支持WebSocket");
	}
    
    $("#submit").click(function(){
    	var content = $("#content").val();
    	console.log(content);
	});
})